<?php

class Category_m extends MY_Model
{
    protected $_table = 'categories';   
    
    function get_sorted_categories($data)
    {
        $tdata=array();
        return $this->get_children($data, 0, 0, &$tdata, 0);
        
    }
    function get_children($array , $parent_id = 0 , $deep , &$tdata=array(),$level = 0)
    {
        foreach ($array as $value){
        
            if($value->parent_id==$parent_id){
                $n = $deep + 1;
                $value->deep = $deep;
                if(0 != intval($level) && $deep >= intval($level))
                {
                    continue;
                }
                $tdata[]=$value;
                $this->get_children($array, $value->id, $n, $tdata, $level);
            }
        }
        return $tdata;
    }
    // Public methods
    public function get_categories()
    {
        $this->db->select('id, name, url_name, description');
            
        $query = $this->db->get($this->_table);
            
        if ($query->num_rows() > 0)
        {
            return $query->result();
        }
    }
    
    public function get_categories_by_ids($category_ids = null)
    {
        if($category_ids == null)
        {
            return null;
        }
        $this->db->select('id, name, url_name, description');
        $this->db->where_in('id', $category_ids);
            
        $query = $this->db->get($this->_table);
    
        if ($query->num_rows() > 0)
        {
            return $query->result();
        }
    }

    public function get_category($id)
    {
        $this->db->select('id, name, description, parent_id, position, status');
        $this->db->where('id', $id);
            
        $query = $this->db->get($this->_table, 1);
            
        if ($query->num_rows() > 0)
        {
            return $query->row();
        }
    }     

    public function get_categories_by_parentid($id)
    {
        $this->db->select('id, name, url_name, parent_id, position, status');
        $this->db->from($this->_table . ' categories');
        $this->db->order_by('position', 'ASC');
        $this->db->order_by('id', 'ASC');
        
        $query = $this->db->get();
            
        if ($query->num_rows() > 0)
        {            
            $current_category = array();
            $tdata=array();
            
            foreach ($query->result() as $row ){
                if($row->id == $id)
                {
                    $current_category[0] = $row;
                }
                $data[]=$row;
            }
            
            $parent_categories =  $this->get_children($data, $id, 0, &$tdata, $level = 0);
            return array_merge($current_category, $parent_categories);
        }
        else
        {
            return null;
        }
    }     
    

    public function create_category()
    {
        $data = array
                    (
                        'name' => $this->input->post('name'),
                        'url_name' => url_title($this->input->post('name')),
                        'description' => $this->input->post('description'),
                        'parent_id' => $this->input->post('parent_id'),
                        'status' => $this->input->post('status'),
                        'position' => intval($this->input->post('position'))
                    );

        $this->db->insert($this->_table, $data);
    }

    public function edit_category()
    {
        $data = array
                    (
                        'name' => $this->input->post('name'),
                        'url_name' => url_title($this->input->post('name')),
                        'description' => $this->input->post('description'),
                        'parent_id' => $this->input->post('parent_id'),
                        'status' => $this->input->post('status'),
                        'position' => intval($this->input->post('position'))
                    );

        $this->db->where('id', $this->input->post('id'));
        $this->db->update($this->_table, $data);
    }

    public function delete_category($id)
    {
        $categories = $this->get_categories_by_parentid($id);
        $del_ids = array();
        
        foreach($categories as $key => $value){
            $del_ids[] = $value->id;
        }

        if(!empty($del_ids))
        {
            $this->db->where_in('id', implode(",", $del_ids));
            $this->db->delete($this->_table);
        }
    }
}

/* End of file categories_model.php */
/* Location: ./application/modules/admin/models/categories_model.php */